﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
///Topic 的摘要说明
/// </summary>
public class Topic
{
    public Topic()
    {
        replies = new List<Reply>();
        m = new MemDisplay();
    }

    public Topic(int tid)
    {
        replies = new List<Reply>();
        this.tid = tid;
        GetData gd = new GetData();
        threads t = gd.GetOneThread(tid);
        if(t!=null)
        {
            isExist = true;
            title = t.title;
            posttime = t.posttime;
            m = new MemDisplay(gd.GetOneMember(t.uid));
            text = t.text;
            fid = t.fid;
            fName = gd.GetForum(fid).name;
            status = t.status;
            ProbID = t.ProbID;
        }
        else
        {
            isExist = false;
        }

    }

    public Topic(threads t)
    {
        GetData gd = new GetData();
        replies = new List<Reply>();
        m = new MemDisplay(gd.GetOneMember(t.uid));
        tid = t.tid;
        title = t.title;
        posttime = t.posttime;
        lastreptime = t.lastreplytime;
        viewnum = t.viewnum;
        replynum = t.replynum;
        fid = t.fid;
        status = t.status;
        ProbID = t.ProbID;
    }

    #region 变量声明

    public int tid;
    public string title;
    public string text;
    public int uid;
    public int fid;
    public string fName;
    public DateTime posttime;
    public string postip;
    public int viewnum;
    public int replynum;
    public DateTime lastreptime;
    public DateTime? edittime;
    public int? edituid;
    public int status;
    public int ishighlight;
    public int isjinghua;
    public int istop;
    public int? ProbID;
    public List<Reply> replies;
    public MemDisplay m;
    public int postCount = 0;
    public int curNum = 0;
    private static int STEP = 20;
    public bool isExist;
    #endregion


    //fid,uid

    public void GetReplies()//获取所有回复
    {
        GetData gd = new GetData();
        List<posts> po = gd.GetPosts(this.tid);
        if (po == null)
            return;
        foreach (posts p in po)
        {
            MemDisplay m = new MemDisplay(gd.GetOneMember(p.uid));
            Reply rep = new Reply(p, m);
            replies.Add(rep);
        }
    }

    public void GetReplies(int pageSize, int pageIndex)//获取指定页得回复
    {
        GetData gd = new GetData();
        List<posts> po = gd.GetPosts(this.tid, pageSize, pageIndex);
        if (po == null)
            return;
        foreach (posts p in po)
        {
            MemDisplay m = new MemDisplay(gd.GetOneMember(p.uid));
            Reply rep = new Reply(p, m);
            replies.Add(rep);
        }
    }

    public void SetProbID(int probID)//设置题号
    {
        this.ProbID = probID;
    }

    public void GetRepliesCount()//获取所有回复个数
    {
        GetData gd = new GetData();
        this.postCount = gd.GetPostCount(this.tid);
    }

    public void CutTitle(int len)//截断标题
    {
        if (this.title.Length > len)
        {
            this.title = this.title.Substring(0, len);

            this.title += "...";
        }
    }

    public void SetTop()//设置置顶
    {
        this.istop = 1;
        //save data
    }

    public void SetJinghua()//设置精华
    {
        this.isjinghua = 1;
        //save data
    }

    public void SetHighLight()//设置高亮
    {
        this.isjinghua = 1;
        //save data
    }

    public static void Del(int tid)//删除
    {
        try
        {
            SaveData.DelThread(tid);
        }
        catch (System.Exception ex)
        {
        	
        }
    }

    public void Move()//移动
    {

    }

    public void Publish()//发布
    {
        try
        {
            this.tid = SaveData.NewTopic(this);
        }
        catch (System.Exception ex)
        {
            //Error
        }
    }

    public void BeenVisited()
    {
        try
        {
            SaveData.IncTopicViewNum(this.tid);
        }
        catch (Exception ex)
        {
            
            //出错处理
        }
    }

    public void BeenReplied()
    {
        try
        {
            SaveData.IncTopicRepNum(this.tid,DateTime.Now);
        }
        catch (Exception ex)
        {

            //出错处理
        }
    }

     public static string getLastTopic()
    {
        string R = "";
        GetData gd = new GetData();
        List<threads> th = gd.GetLastPostThreads(10);
        if (th!=null)
        {
		if(th.Count() != 0)
{

            R += "<fieldset id=\"LastTopicList_wrap\"><legend>最新帖子</legend>";


            R += "<ul id=\"LastTopicList\">";
            foreach (threads t in th)
            {
                if (t.title.Length > 10)
                {
                    t.title = t.title.Substring(0, 10);
                    t.title += "...";
                }
                R += "<li><a href=\"ViewTopic.aspx?tid=" + t.tid + "&page=1\">" + t.title + "</a></li>";
            }
            R += "</ul></fieldset>";
        }
}

        return R;
    }
}
